home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rjs.lha / RJS / NDR / src / NDR_send.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-14  |  3.4 KB  |  148 lines

  1. #include <string.h>
  2. #include <sys/types.h>
  3. #include <sys/uio.h>
  4. #include "NDR.h"
  5.  
  6. extern "C" {
  7.     unsigned short htons(unsigned short);
  8. };
  9.  
  10.  
  11. RJS_Transport &operator<<(RJS_Transport &tr, RJS_NDR_send &ndr)
  12. {
  13.     unsigned short len=htons(ndr.raw_length());
  14.     struct iovec iov[2];
  15.     iov[0].iov_base = (char *) &len;
  16.     iov[0].iov_len  = sizeof(unsigned short);
  17.     iov[1].iov_base = ndr.raw_data();
  18.     iov[1].iov_len  = ndr.raw_length();
  19.     tr.writev(iov,2);
  20.     return tr;
  21. }
  22.  
  23. ostream &operator<<(ostream &os, RJS_NDR_send &ndr)
  24. {
  25.     cout << "local_endian() => " << ndr.local_endian() << endl;
  26.     cout << "local_character() => " << ndr.local_character() << endl;
  27.     cout << "local_floating() => " << ndr.local_floating() << endl;
  28.     cout << "raw_length() => " << ndr.raw_length() << endl;
  29.     for (int i=0; i< ndr.raw_length(); i++) {
  30.         cout << i << "\t" << int(*(ndr.raw_data()+i)) << endl;
  31.     }
  32.     return os;
  33. }
  34.  
  35. RJS_NDR_send &operator<<(RJS_NDR_send &ndr, RJS_XNDR &x) {
  36.     x.to_external(ndr);
  37.     return ndr;
  38. }
  39.  
  40. RJS_NDR_send::RJS_NDR_send()
  41.     start_data=NULL; 
  42.     end_data=NULL; 
  43.     end_alloc=NULL; 
  44. }
  45.  
  46.  
  47. void RJS_NDR_send::reset()
  48. {
  49.     end_data = start_data+2;
  50. }
  51.  
  52. void RJS_NDR_send::init(char *buffer, int size)
  53. {
  54.     int align=((unsigned int)(buffer)) % sizeof(double);
  55.     if (align) buffer += (sizeof(double)-align);    
  56.     start_data  = buffer;
  57.     end_data    = buffer;
  58.     end_alloc   = buffer+size;
  59.     (*end_data++)   = (local_endian() << 4)  | (local_character());
  60.     (*end_data++)   =  (local_floating());
  61.  
  62. }
  63.  
  64. RJS_NDR_send &RJS_NDR_send::insert(char *d, int count)
  65. {
  66.     (*this) << count;
  67.     strncpy(end_data,d,count);
  68.     end_data += count;
  69.     return *this;
  70. }
  71.  
  72.  
  73. RJS_NDR_send &operator<<(RJS_NDR_send &ndr,char ch)
  74. {
  75.     *ndr.end_data++ = ch;
  76.     return ndr;
  77. }
  78.  
  79. RJS_NDR_send &operator<<(RJS_NDR_send &ndr,unsigned char ch)
  80. {
  81.     *ndr.end_data++ = ch;
  82.     return ndr;
  83. }
  84.  
  85. RJS_NDR_send &operator<<(RJS_NDR_send &ndr,short i)
  86. {
  87.     if (((unsigned int)(ndr.end_data)) % sizeof(short)) ndr.end_data++; 
  88.     * (short *) ndr.end_data = i;
  89.     ndr.end_data += sizeof(short);
  90.     return ndr;
  91. }
  92.  
  93. RJS_NDR_send &operator<<(RJS_NDR_send &ndr,unsigned short i)
  94. {
  95.     if (((unsigned int)(ndr.end_data)) % sizeof(unsigned short)) ndr.end_data++;
  96.     * (unsigned short *) ndr.end_data = i;
  97.     ndr.end_data += sizeof(unsigned short);
  98.     return ndr;
  99. }
  100.  
  101. RJS_NDR_send &operator<<(RJS_NDR_send &ndr,int i)
  102. {
  103.     int align=((unsigned int)(ndr.end_data)) % sizeof(int);
  104.     if (align) ndr.end_data += (sizeof(int)-align); // align on natural
  105.     * (int *) ndr.end_data = i;
  106.     ndr.end_data += sizeof(int);
  107.     return ndr;
  108. }
  109.  
  110. RJS_NDR_send &operator<<(RJS_NDR_send &ndr,unsigned int i)
  111. {
  112.     int align=((unsigned int)(ndr.end_data)) % sizeof(unsigned int);
  113.     if (align) ndr.end_data += (sizeof(unsigned int)-align);
  114.     * (unsigned int *) ndr.end_data = i;
  115.     ndr.end_data += sizeof(unsigned int);
  116.     return ndr;
  117. }
  118.  
  119. RJS_NDR_send &operator<<(RJS_NDR_send &ndr,float d)
  120. {
  121.     int align=((unsigned int)(ndr.end_data)) % sizeof(float);
  122.     if (align) ndr.end_data += (sizeof(float)-align);
  123.     * (float *) ndr.end_data = d;
  124.     ndr.end_data += sizeof(float);
  125.     return ndr;
  126. }
  127.  
  128. RJS_NDR_send &operator<<(RJS_NDR_send &ndr,double d)
  129. {
  130.     int align=((unsigned int)(ndr.end_data)) % sizeof(double);
  131.     if (align) ndr.end_data += (sizeof(double)-align);
  132.     * (double *) ndr.end_data = d;
  133.     ndr.end_data += sizeof(double);
  134.     return ndr;
  135. }
  136.  
  137. RJS_NDR_send &operator<<(RJS_NDR_send &ndr,const char *str)
  138. {
  139.     unsigned short len=strlen(str)+1;
  140.     ndr << len;
  141.     strcpy(ndr.end_data,str);
  142.     ndr.end_data += len;
  143.     return ndr;
  144. }
  145.  
  146.  
  147.